package vip.bblog.cunadmin.modules.system.service.impl;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import vip.bblog.cunadmin.common.entity.PageResult;
import vip.bblog.cunadmin.modules.system.dto.ProjectParams;
import vip.bblog.cunadmin.modules.system.entity.Customer;
import vip.bblog.cunadmin.modules.system.entity.Publisher;
import vip.bblog.cunadmin.modules.system.mapper.CustomerMapper;
import vip.bblog.cunadmin.modules.system.mapper.PublisherMapper;
import vip.bblog.cunadmin.modules.system.service.CustomerService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *门店
 * </p>
 * @author yy
 * @since
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PublisherMapper publisherMapper;

    @Override
    public PageResult<List<Customer>> listPage(ProjectParams params) {
        Page<Customer> page = params.getIPage();
        QueryWrapper<Customer> queryWrapper = new QueryWrapper();
        //根据姓名查询
        if (StringUtils.isNotBlank(params.getName())) {
            queryWrapper.eq("name", params.getName());
        }
        if (StringUtils.isNotBlank(params.getPhone())){
            queryWrapper.eq("phone", params.getPhone());
        }
        if (StringUtils.isNotBlank(params.getIdcard())){
            queryWrapper.like("idcard", params.getIdcard());
        }
        if (StringUtils.isNotBlank(params.getCardNum())){
            queryWrapper.like("card_num", params.getCardNum());
        }
        if (params.getIsCancer() != null){
            queryWrapper.like("is_cancer", params.getIsCancer());
        }
        // 激活时间段查询
        if (StringUtils.isNotBlank(params.getStartTime())){
            queryWrapper.between("activation_time", params.getStartTime(), params.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        Page<Customer> pageResult = this.page(page, queryWrapper);
        List<Customer> list = pageResult.getRecords();

        return PageResult.success(list, pageResult.getTotal());
    }

    public List<String> getPulisher(String[] ids){
        QueryWrapper<Publisher> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        List<Publisher> list = publisherMapper.selectList(queryWrapper);
        List<String> list1 = new ArrayList<>();
        for(Publisher p:list){
            list1.add(p.getName());
        }
        return list1;
    }

    @Override
    public void saveAll(MultipartFile excelFile) throws Exception {
        //参数1:文件输入流 参数2:实体类类型 参数3:导入的配置信息
        ImportParams params = new ImportParams();
        params.setTitleRows(0);  //文件标题所占行数
        params.setHeadRows(1);  //文件的列名所占的行数
        List<Customer> list = ExcelImportUtil.importExcel(excelFile.getInputStream(), Customer.class, params);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ctime = formatter.format(new Date());
        for (Customer eqt : list) {
//             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//             String date = df.format(new Date(eqt.getDate()));
//            eqt.setDate(date);
            eqt.setCreateTime(ctime);
            customerMapper.insert(eqt);
        }
    }

    @Override
    public IPage<Customer> getMyMonth(ProjectParams entity) {
        if(entity != null){
            if(StringUtils.isNotBlank(entity.getInPulisherId())) {
                String[] ids = entity.getInPulisherId().split(",");
                List<String> pulisher = getPulisher(ids);
                entity.setPulisherList(pulisher);
            }
        }
        Page<Customer> page = new Page<>(entity.getPage(), entity.getPageSize());
        return customerMapper.getMyMonth(page,entity);
    }

    @Override
    public IPage<Customer> getMyDay(ProjectParams entity) {
        if(entity != null){
            if(StringUtils.isNotBlank(entity.getInPulisherId())) {
                String[] ids = entity.getInPulisherId().split(",");
                List<String> pulisher = getPulisher(ids);
                entity.setPulisherList(pulisher);
            }
        }
        Page<Customer> page = new Page<>(entity.getPage(), entity.getPageSize());
        return customerMapper.getMyDay(page,entity);
    }
}
